<template>
  <a-modal
    title="强制下线设置"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <!-- 参数 -->
        <a-form-item
          label="禁用时长/小时"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select placeholder="请选择禁用时长/小时" v-decorator="['offlineTime', {rules: [{ required: true, message: '请选择禁用时长/小时' }]}]" >
            <a-select-option v-for="(item,index) in offlineTimeDict" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="封号理由"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input placeholder="请输入理由"  v-decorator="['offlineReason', {rules: [{required: false, message: '请输入理由'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { appUserOfflineEdit } from '@/api/modular/app/user/appUserManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        offlineTimeDict: [],
        offlineTime: ''
      }
    },
    created () {
      // 初始化字典下来
      this.sysDictTypeDropDown()
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              offlineTime: this.offlineTime
            }
          )
        }, 100)
      },
      /**
       * 获取字典数据
       */
      sysDictTypeDropDown () {
        this.offlineTimeDict = this.$options.filters['dictData']('offline_time')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            appUserOfflineEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
